#include "tools.h"
#include "manager_data.h"
#include "manager.h"


void ManagerData::load(void)
{
	int fd = open("../data/manager.dat",O_RDONLY|O_CREAT,0644);
	int ret;
	do
	{
		ret = 0;
		int id;
		char name[20];
		char password[20];
		ret += read(fd,&id,sizeof(id));
		ret += read(fd,&name,sizeof(name));
		ret += read(fd,&password,sizeof(password));
		if(0 < ret)
		{
			Manager manager(id,name,password);
			vecManager.push_back(manager);
		}
	}while(ret != 0);
	close(fd);
	return;
}

void ManagerData::save(void)
{
	int fd = open("../data/manager.dat",O_WRONLY|O_TRUNC|O_CREAT,0644);
	if(0 > fd)
	{
		return;
	}
	vector<Manager>::iterator it;
	for( it = vecManager.begin(); it != vecManager.end(); it++ )
	{
		write(fd,&it->id,sizeof(it->id));
		write(fd,&it->name,sizeof(it->name));
		write(fd,&it->password,sizeof(it->password));
	}
	close(fd);
	return;
}

